const assert = require("http-assert");
const authMiddelWear = require("../middlewares/authMiddelware");
const express = require("express");

module.exports = app => {
    const express = require("express");

    app.use(async (req, res, next) => {
        console.log(req.url);
        next();
    })

    const router = express.Router({
        mergeParams: true,//接受父级路由参数
    });

    //新增管理员
    const {createAdmin} = require("../router-hander/index");
    router.post("/create", createAdmin);

    //管理员列表
    const {adminList} = require("../router-hander/index");
    router.get("/list", adminList);

    //删除管理员
    const {deleteAdmin} = require("../router-hander/index");
    router.post("/delete/:id", deleteAdmin);

    //用户详细
    const {adminMessage} = require("../router-hander/index");
    router.get("/message/:id?", adminMessage);

    //编辑用户详细
    const {messageEdit} = require("../router-hander/index");
    router.post("/message", messageEdit);

    //上传模块
    const multer = require("multer");
    const upload = multer({dest: __dirname + "./../uploads"})
    //图片上传
    const {uploadImages} = require("../router-hander/index");
    router.post("/uploadImages", upload.single("avatar"),uploadImages);

    //验证token
    const authMiddelWear = require("./../middlewares/authMiddelware");
    //子路由
    app.use("/admin", authMiddelWear(), router);

    //页面验证
    const AdminMessage = require("../models/AdminMessage")
    app.get("/authorization", authMiddelWear(), async (req, res) => {
        const userMessage = await AdminMessage.findOne({userId: req._id});
        console.log("userMessage", userMessage);
        res.send({message: {userMessage}});
    });

    //挂载静态图片
    app.use("/uploads",express.static(__dirname + "./../uploads"))

    //登录路由
    const {loginHander} = require("../router-hander/index")
    app.post("/login", loginHander);

    //错误级别中间件
    app.use(async (err, req, res, next) => {
        console.log("@@@", err);
        res.status(err.statusCode || 500).send({
            message: err.message,
        })
    })
}